package com.sfdk.locale
{
import com.sfdk.events.IOEvent;
import com.sfdk.io.GenericIO;

import flash.events.Event;

public class DateFormatter extends GenericIO
	{
		private static var dateFormatter_ : DateFormatter;
		
		private var dateFormating_ : XML;
		private var currentLocale_ : String = "FR";
		
		public function DateFormatter(locale : String) : void
		{
			// Initialisation de la locale
			dateFormatter_ = this;
			currentLocale_ = locale.toLowerCase();
			addEventListener(IOEvent.LOAD_COMPLETE, loadDateFormating, false, 0, true);
			
			super("resources/messages/"+currentLocale_+"/dateFormatting.xml");
		}
		
		private function loadDateFormating(e : Event) : void
		{
			dateFormating_ = getFileData();
		}
		
		public function get dateFormating() : XML
		{
			return dateFormating_;
		}
		
		public static function formatSeason(seasonNum : int) : String
		{
			var saisonStr : String = "";
			if(dateFormatter_.dateFormating != null)
			{
				var localeRoot : XMLList = dateFormatter_.dateFormating.elements(dateFormatter_.currentLocale_);
				if(localeRoot.length() != 0)
				{
					var saison : XMLList = (localeRoot[0] as XML).elements("saison");
					saisonStr = (saison[seasonNum] as XML);
				}
			}
			return saisonStr;
		}
		
		public static function formatDay(day : int) : String
		{
			var dayStr : String = day.toString();
			if(dayStr.length == 1)
				return "0"+dayStr;
			else
				return dayStr;
		}
		
		public static function formatYear(year : int) : String
		{
			var yearStr : String = year.toString();
			while(yearStr.length < 4)
			{
				yearStr = "0" + yearStr;
			}
			return yearStr;
		}
		
		public static function formatDate(day : int, seasonNum : int, year : int) : String
		{
			return formatDay(day) + " " + formatSeason(seasonNum) + " " + formatYear(year);
		}
	}
}